<?php

namespace App\Http\Controllers\Traits;

use App\Library\Sms\TopClient;
use App\Library\Sms\AlibabaAliqinFcSmsNumSendRequest;
use App\Model\Equipment;
use App\Model\Helper;
use App\Model\Sms;
use App\Model\User;
use \Exception;
use JPush\Client;
use JPush;
use Redis;

trait CommonControllerTrait {




    protected function _Redis()
    {
        $redis=new Redis();
        $redis->connect(config('app.redishost'),config('app.redisport'));
        $redis->auth(config('app.redispwd'));
        $redis->select(1);
//        $redis->connect('120.26.220.134',config('app.redispost'));
//        $redis->auth(config('app.redispwd'));
//        $redis->auth('lggtest');
        return $redis;
    }

    protected function _sendAlert($phone,$site,$alert){
        $c=new TopClient();
        $c->appkey='23372138';
        $c->secretKey='48480a83996ec04dc36adf1837cc189d';
        $c->format="json";
        $req= new AlibabaAliqinFcSmsNumSendRequest();
        $req->setSmsType("normal");
        $req->setSmsFreeSignName("芯芸");
        $req->setSmsParam("{\"name\":\"".$phone."\",\"sites\":\"".$site."\",\"alert\":\"".$alert."\"}");
        $req->setRecNum($phone);
        $req->setSmsTemplateCode("SMS_77060004");
        $resp = $c->execute($req);
        $res= $this->objectArray($resp);
        if(@$res['result']['err_code'] == 0) {
            return true;
        }
    }

    private function objectArray($array){
        if(is_object($array)){
            $array = (array)$array;
        }
        if(is_array($array)){
            foreach($array as $key=>$value){
                $array[$key] = $this->objectArray($value);
            }
        }
        return $array;
    }
}